/*
 *所有关于fc_yszk类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*fc_yszkService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using VOL.Core.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using System.Collections.Generic;
using VOL.Core.DBManager;
using System;
using OfficeOpenXml;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System.IO;

namespace VOL.Finance.Services
{
    public partial class fc_yszkService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Ifc_yszkRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public fc_yszkService(
            Ifc_yszkRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();
        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecuting = (fc_yszk order, object addList, object updateList, List<object> delKeys) =>
            {
                var fc_yszk_id = order.cw_fc_yszk_id;
                var dd = DBServerProvider.DbContext.Set<fc_yszk>().Where(x => x.cw_fc_yszk_id == fc_yszk_id).Select(s => s.isapproved).ToList().FirstOrDefault();
                if (dd == 1)
                {
                    return webResponse.Error("此数据已审核通过，不能再进行编辑操作！");
                }

                return webResponse.OK();
            };
            return base.Update(saveModel);
        }

        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            DelOnExecuting = (object[] _keys) =>
            {
                var id = Guid.Parse(keys[0].ToString());
                var dd = DBServerProvider.DbContext.Set<fc_yszk>().Where(x => x.cw_fc_yszk_id == id).Select(s => s.isapproved).ToList().FirstOrDefault();
                if (dd == 1)
                {
                    return webResponse.Error("此数据已审核通过，不能再进行编辑操作！");
                }
                return webResponse.OK();
            };
            return base.Del(keys, delList);
        }

        public override WebResponseContent Import(List<IFormFile> files)
        {
            string yearandmonth = CommonData.yearMonthGlobals;
            ImportOnExecuting = (List<fc_yszk> list) =>
            {
                var dapper = DBServerProvider.SqlDapper;
                try
                {
                    DateTime date = Convert.ToDateTime(yearandmonth);
                    dapper.BeginTrans();
                    string sql = "delete from cw_fc_yszk where duedate=@value";
                    var data = dapper.ExcuteNonQuery(sql, new { value = date });

                    dapper.Commit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    dapper.Rollback();
                }
                return webResponse.OK();
            };
            return base.ImportYszk(files);
        }

        
    }
}
